package reduce.fz.com.reduce.activity;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.adapter.WitnessAdapter;
import reduce.fz.com.reduce.init.RecycleOper;
import reduce.fz.com.reduce.util.FileUtil;

public class SendWitnessActivity extends BaseToolBarActivity implements View.OnClickListener {
    private EditText witness_desc;
    private RecyclerView witness_image_list;
    public static final int TAKE_PICTURE = 00000;
    public WitnessAdapter witnessAdapter;
    private PopupWindow pop;
    private TextView taskPhto;
    private TextView selectPhoto;
    private String path = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_witness);
        RecycleOper recycleOper = new RecycleOper(this, witness_image_list);
        recycleOper.setGmDefault(3);

    }

    @Override
    public void initWidget() {
        witness_desc = (EditText) this.findViewById(R.id.witness_desc);
        witness_image_list = (RecyclerView) this.findViewById(R.id.witness_image_list);
    }

    @Override
    public void initListener() {


    }

    public void tokephone() {
        showPopWindow();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (resultCode) {
            case 1:
                if (data != null) {
                    //取得返回的Uri,基本上选择照片的时候返回的是以Uri形式，但是在拍照中有得机子呢Uri是空的，所以要特别注意
                    Uri mImageCaptureUri = data.getData();
                    //返回的Uri不为空时，那么图片信息数据都会在Uri中获得。如果为空，那么我们就进行下面的方式获取
                    if (mImageCaptureUri != null) {
                        Bitmap image;
                        try {
                            //这个方法是根据Uri获取Bitmap图片的静态方法
                            image = MediaStore.Images.Media.getBitmap(this.getContentResolver(), mImageCaptureUri);
                            if (image != null) {
                                String filepath = path + System.currentTimeMillis();
                                FileUtil.savePicToSdcard(image, path, System.currentTimeMillis() + "");
                                // photo.setImageBitmap(image);
                                witnessAdapter.addImageUrl(path);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        Bundle extras = data.getExtras();
                        if (extras != null) {
                            //这里是有些拍照后的图片是直接存放到Bundle中的所以我们可以从这里面获取Bitmap图片
                            Bitmap image = extras.getParcelable("data");
                            if (image != null) {
                                String filepath = path + System.currentTimeMillis();
                                // photo.setImageBitmap(image);
                                FileUtil.savePicToSdcard(image, path, System.currentTimeMillis() + "");
                                witnessAdapter.addImageUrl(path);
                            }
                        }
                    }

                }
                break;
            default:
                break;

        }
    }


    public void showPopWindow() {
        View v = this.getLayoutInflater().inflate(R.layout.take_photo_view, null);
        pop.update();
        pop.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        pop.setTouchable(true); // 设置popupwindow可点击
        pop.setOutsideTouchable(true);  // 设置popupwindow外部可点击
        pop.setFocusable(true); //获取焦点
        pop.showAsDropDown(v, 100, 100);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_take_photo:
                try {
                    //拍照我们用Action为MediaStore.ACTION_IMAGE_CAPTURE，
                    //有些人使用其他的Action但我发现在有些机子中会出问题，所以优先选择这个
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(intent, 1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_pick_photo:
                try {
                    //选择照片的时候也一样，我们用Action为Intent.ACTION_GET_CONTENT，
                    //有些人使用其他的Action但我发现在有些机子中会出问题，所以优先选择这个
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(intent, 2);
                } catch (ActivityNotFoundException e) {

                }
                break;
            case R.id.btn_cancel:
                finish();
                break;
            default:
                break;
        }

    }
}
